package com.wbdgj.ui.store.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.MoreZdAdapter;
import com.wbdgj.adapter.PointsMallAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.store.PointsMallDetailsActivity;
import com.wbdgj.utils.MyGridView;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;


/**
 * Created by Administrator on 2018/5/2.
 */

public class PointsMall01Fragment extends BaseFragment{

    private Context context;
    private String ID;

    private PointsMallAdapter pointsMallAdapter;

    @BindView(R.id.demo_listview)
    MyGridView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int page = 1;
    private int totalPage;

    @Override
    protected int getLayout() {
        return R.layout.ui_points_mall_fragment;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        Bundle bundle = getArguments();
        ID = bundle.getString("ID");

        getGoodsList();
        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if (!pointsMallAdapter.hasNoData){
                    startActivity(new Intent(context, PointsMallDetailsActivity.class).putExtra("ID", decimalFormat.format(pointsMallAdapter.getItem(i).get("Id"))));
                }
            }
        });
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getGoodsList();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if ((page*10) >= totalPage){
                    ToastUtils.toastShort("已无更多");
                }else{
                    page = page + 1;
                    getGoodsList();
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });

    }


    @Override
    public void fetchData() {

    }

    private void getGoodsList() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getGoodsList(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID, "1", page+"", "10");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        ArrayList<LinkedTreeMap> linkedTreeMapList = (ArrayList<LinkedTreeMap>)linkedTreeMap.get("data");
                        if (page == 1){
                            pointsMallAdapter = new PointsMallAdapter(context, linkedTreeMapList);
                            myPullListView.setAdapter(pointsMallAdapter);

                        }else{
                            pointsMallAdapter.loadMoreListView(linkedTreeMapList);
                            pointsMallAdapter.notifyDataSetChanged();
                        }
                    }

                    if(linkedTreeMap.containsKey("Pager")){
                        LinkedTreeMap pagerLink = (LinkedTreeMap) linkedTreeMap.get("Pager");
                        if(pagerLink.containsKey("total")){
                            totalPage = Integer.parseInt(decimalFormat.format(pagerLink.get("total")));
                        }
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

}
